package sa.com.sure.ejtimaa.database;


import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.model.MemberInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class Members {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "Members";
	
    public static final String UserIDField = "UserID"; 
    public static final String FullNameField = "FullName";
    public static final String MobileField = "Mobile";
    public static final String UserNameField = "UserName";
    public static final String MemberIDField = "MemberID";
    public static final String EmailField = "Email";
    public static final String ActiveField = "Active";
    
    private String[] allColumns = { UserIDField, FullNameField,MobileField,UserNameField,MemberIDField ,EmailField , ActiveField};
    
    Cursor cursor = null;
    
    public Members(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static Members Instance = new Members(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
	
	public void addItem(MemberInfo item) { 
      
		try
		{
				
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(UserIDField, item.GetUserID()); 
	        values.put(FullNameField, item.GetFullName());  
	        values.put(MobileField, item.GetMobile());
	        values.put(UserNameField, item.GetUserName());
	        values.put(MemberIDField, item.GetMemberID());
	        values.put(EmailField, item.GetEmail());
	        values.put(ActiveField, item.GetActive());
	                
	        dbHelper.db.insert(TABLE_Name, null, values); 
        
		}
		finally
		{
			close();
		}

    } 

	
	public MemberInfo GetItemByUserID(String UserID) throws SQLException{

		
		try{
			
				open();
		
				MemberInfo item = null;
			
			    cursor = dbHelper.db.query(TABLE_Name,allColumns, UserIDField + "='" + UserID +"'",null,null,null,null);
	        	if (cursor != null) {
		        	cursor.moveToFirst();	        	
	        	item =  getItemInfo(cursor);
		     
	        }	                
	        
	    	return 	item;    	
		}
		finally
		{
			close();
		}

	    
	}

	

	public MemberInfo GetItemByUserName(String UserName) throws SQLException{
	
		try{			
		
			open();
	
			MemberInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, UserNameField + "='" + UserName +"'",null,null,null,null);
		    
	        if(cursor.moveToFirst())	        	
	        	item =  getItemInfo(cursor);
	             		                       
    	return 	item;
    	
		}
		finally
		{
			close();
		}

	    
	}
	
	public MemberInfo GetItemByMemberID(String MemberID) throws SQLException{

		try{			
		
			open();
		
			MemberInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MemberIDField + "='" + MemberID +"'",null,null,null,null);	        	
        	if (cursor.moveToFirst())	        	
        		item =  getItemInfo(cursor);		     	               
	        
	    	return 	item;    	
		}
		finally
		{
			close();
		}	    
	}

	
	 public boolean updateItemByUserName(MemberInfo item) 
	 {		 
		try
		{			
		 	open();
		 	
		    ContentValues values = new ContentValues();
		    
	        values.put(UserIDField, item.GetUserID()); 
	        values.put(FullNameField, item.GetFullName());  
	        values.put(MobileField, item.GetMobile());        
	        values.put(MemberIDField, item.GetMemberID());
	        values.put(EmailField, item.GetEmail());
	        values.put(ActiveField, item.GetActive());
    
	        
		    boolean res =   dbHelper.db.update(TABLE_Name, values, UserNameField + "='" + item.GetUserName() + "'", null) > 0;			    
		    
		    return res;
			
		}
		finally
		{
			close();
		}		 
	 }

	 public boolean updateItemByMemberID(MemberInfo item) {

		try
		{
		 	open();
		 	
		    ContentValues values = new ContentValues();		    

	        values.put(UserIDField, item.GetUserID()); 
	        values.put(FullNameField, item.GetFullName());  
	        values.put(MobileField, item.GetMobile());
	        values.put(UserNameField, item.GetUserName());	        
	        values.put(EmailField, item.GetEmail());
	        values.put(ActiveField, item.GetActive());
		    
		    boolean res =   dbHelper.db.update(TABLE_Name, values, MemberIDField + "='" + item.GetMemberID() + "'", null) > 0;		    
		    
		    return res;
			
		}
		finally
		{
		    close();
		}		 
    }

	 
	
    private MemberInfo getItemInfo(Cursor cursor) {  
    	
    	MemberInfo itemInfo = new MemberInfo();
    	
    	itemInfo.SetUserID(cursor.getString(cursor.getColumnIndex(UserIDField))); 
    	itemInfo.SetFullName(cursor.getString(cursor.getColumnIndex(FullNameField)));
    	itemInfo.SetMobile(cursor.getString(cursor.getColumnIndex(MobileField)));
    	itemInfo.SetUserName(cursor.getString(cursor.getColumnIndex(UserNameField)));
    	itemInfo.SetEmail(cursor.getString(cursor.getColumnIndex(EmailField)));
    	itemInfo.SetActive(cursor.getInt(cursor.getColumnIndex(ActiveField)) == 1);
    	
		return itemInfo;
	}
}
